package com.ymatou.iapi.liveinfo.testcase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveinfo.parameter.GetActivityIdsBySellerIdsBean;
import com.ymatou.iapi.liveinfo.service.GetActivityIdsBySellerIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.database.mongo.jproduct.LivesWapper;
import com.ymttest.utils.EnvSetup;
/**
*根据卖家id列表获取正在进行中直播列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetActivityIdsBySellerIds{
	//创建正在进行的直播的用户
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	//创建正在进行的直播的用户
	private static int sellerId4 = Integer.parseInt(EnvSetup.getData("productsellerid6"));
	private static String sellerName4 = EnvSetup.getData("productsellername6");
	//创建已经结束的直播的用户
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("productsellerid5"));
	private static String sellerName2 = EnvSetup.getData("productsellername5");
	//卖家没有创建直播的用户
	private static int sellerId3 = Integer.parseInt(EnvSetup.getData("productsellerid7"));
	private static String sellerName3 = EnvSetup.getData("productsellername7");
	private static GetActivityIdsBySellerIdsBean bean;
	private static GetActivityIdsBySellerIdsCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据卖家id列表获取正在进行中直播列表");
	}

	@Before
	public void caseUp() {
		bean=new GetActivityIdsBySellerIdsBean();
		call=new GetActivityIdsBySellerIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityIdsBySellerIds_001() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播ID列表-当前卖家id列表都有正在进行的直播,且卖家id列表长度为1-返回卖家id列表当前正在进行的直播");
		try {
			int[] sellIds=new int[1];
		    LiveCallService.createLiveInProgress(sellerName,sellerId);
		    sellIds[0]=sellerId;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetActivityIdsBySellerIds_002() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播ID列表-当前卖家id列表都有正在进行的直播,且卖家id列表长度为2-返回卖家id列表当前正在进行的直播");
		try {
			int[] sellIds=new int[2];
		    LiveCallService.createLiveInProgress(sellerName,sellerId);
		    LiveCallService.createLiveInProgress(sellerName4,sellerId4);
		    sellIds[0]=sellerId;
		    sellIds[1]=sellerId4;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetActivityIdsBySellerIds_003() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播ID列表-当前卖家id列表有部分正在进行的直播-返回卖家id列表当前部分正在进行的直播");
		try {
			int[] sellIds=new int[2];
		    LiveCallService.createLiveInProgress(sellerName,sellerId);
		    int lid=LiveCallService.createLiveInProgress(sellerName4,sellerId4).getJSONObject("Data").getInt("ActivityId");
		    LiveCallService.closeLive(lid, sellerId4);
		    sellIds[0]=sellerId;
		    sellIds[1]=sellerId4;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetActivityIdsBySellerIds_004() {
		Logger.start(false,"根据卖家id列表获取正在进行中直播ID列表-当前卖家id列表没有正在进行的直播-返回空");
		try {
			int[] sellIds=new int[1];
		    sellIds[0]=sellerId3;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyEquals(0, call.getData().getInt("ActivityCount"), "验证直播返回数量ActivityCount为0");
			Logger.verifyEquals("[]",call.GetActivities().toString(), "判断服务器时间是否为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void GetVerify(int[] sellIds) throws Exception 
	{
		LivesWapper wapper=new LivesWapper();
		if(sellIds.length>0){
			Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			ArrayList<Map> lst=wapper.getLivesListBySellerIds(sellIds);
		for(int i=0;i<lst.size();i++){	
			Logger.verifyEquals(lst.get(i).get("sid").toString(), call.GetActivities().getJSONObject(i).getString("SellerId"), "验证直播SellerId是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("lid").toString(), call.GetActivities().getJSONObject(i).getString("ActivityId"), "验证直播ActivityId是否与mongodb一致");
			
		}
	  }
	}
}